// pages/scancopy/copyList/copyList.js
var utils = require("../../../utils/utils.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        config:{
            allowRemove:true,
            allowScale:true,
            allowMove:true,
            allowRotate:true
        },
        cutImg:'',
        imgLength:0
    },
    setCutImg(img){
        if(utils.is_define(img)){
            this.selectComponent("#loading").showLoadingCus();
            try{
                this.selectComponent('#pm').add(img)
                this.selectComponent("#loading").hideLoadingCus();
            }catch(err){
                this.selectComponent("#loading").hideLoadingCus();
            }
           
        }
    },
    showAdvs() {
        this.setData({
          showAdv: true
        })
      },
    hideAdvs(){
        this.setData({
          showAdv: false
        })
    },
    getpictures(e){
        this.setData({
            cutImg:'',
            imgLength:e.detail.layers.length
        })
        this.selectComponent("#loading").hideLoadingCus();
    },
    add() {
        var that = this;
        var code = that.data.code;
        wx.showActionSheet({
            itemList: ['拍照', '相册', '从QQ会话选择'],
            success(res) {
                if (res.tapIndex == 0) {
                    that.getPhoto('camera', code);
                }
                if (res.tapIndex == 1) {
                    that.getPhoto('album', code);
                }
                if (res.tapIndex == 2) {
                    that.getPhoto1(code);
                }
            },
            fail(res) {

            }
        })
    },
    //拍照调取相册
    getPhoto(type) {
        var that = this;
        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: [type],
            sizeType: ['original'],
            success(res) {
                that.selectComponent("#loading").showLoadingCus('请稍候');
                that.uploadFileImg(res.tempFiles[0].tempFilePath);
            }
        })
    },
    getPhoto1() {
        var that = this;
        wx.chooseMessageFile({
            count: 1,
            type: 'image',
            success(res) {
                that.selectComponent("#loading").showLoadingCus('请稍候');
                that.uploadFileImg(res.tempFiles[0].path);
            }
        })
    },
    uploadFileImg(url) {
        var that = this;
        var str = '上传中';
        that.selectComponent("#loading").showLoadingCus(str);
        utils.scanuploadfile('convert/picMatting',url,function (res) {
            var data = JSON.parse(res.data).data;
            if (data.cut == 1) {
                try {
                    that.selectComponent('#pm').add(data.fileUrl)
                } catch (error) {
                    that.selectComponent("#loading").hideLoadingCus();
                    if (error.err_code == 1000) {
                        utils.toast('您最多只能上传五张图片');
                    } else {
                        utils.toast('系统错误，请重试');
                    }
                }
            } else {
                that.selectComponent("#loading").hideLoadingCus();
                wx.showModal({
                    title: '提示',
                    content: '自动裁剪失败，点击确认进行手动裁剪',
                    confirmText: '手动裁剪',
                    cancelText: '取消',
                    success(res) {
                        if (res.confirm) {
                            wx.navigateTo({
                                url: '../copycrop/copycrop?path=' + data.fileUrl,
                            })
                        }
                    }
                })
            }
            that.selectComponent("#loading").hideLoadingCus();
        })
    },
    async save() {
        if (this.data.imgLength == 0) {
            utils.toast('不能打印空白页面')
            return false;
        }
        this.selectComponent("#loading").showLoadingCus('上传中');
        try{
            let res = await this.selectComponent('#pm').save()
            var that = this;
            // utils.uploadfile(res.tempFilePath,'','','', function (res) {
            //     var data = JSON.parse(res.data).data;
            //     wx.hideLoading()
            //     console.log(data)
            //     return false;
            //     wx.navigateTo({
            //         url: '/pages/print/list/list?code=' + that.data.code + '&name=证件复印&path=' + data.printUrl + '&pageNum=' + data.pageTotal + '&fileUrl=' + data.fileUrl + '&serviceMainId=' + that.data.serviceMainId + '&serviceSubtypeId=' + that.data.serviceSubtypeId + '&tagFloor=' + that.data.tagFloor+'&color='+data.color
            //     })
            // })
            utils.uploadfile(res.tempFilePath,res.tempFilePath,'','','',function (res) {
                var data = JSON.parse(res.data).data
                that.selectComponent("#loading").hideLoadingCus();
                  wx.redirectTo({
                    url: '/pages/print/list/list?code=' + that.data.code + '&name=证件复印&path=' + data.printUrl + '&pageNum=' + data.pageTotal +'&fileUrl='+data.fileUrl+'&serviceMainId='+that.data.serviceMainId+'&serviceSubtypeId='+that.data.serviceSubtypeId+'&tagFloor='+that.data.tagFloor+'&color='+data.color+'&orientation=1'
                  })
             })
        }catch(err){
            that.selectComponent("#loading").hideLoadingCus();
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this;
        that.showAdvs();
        that.setData({
            code: options.code,
            serviceMainId: options.serviceMainId,
            tagFloor: options.code,
            serviceSubtypeId: options.serviceMainId
        })
        wx.getSystemInfo({
            success: (result) => {
                var model = result.model;
                if (model == 'iPhone X') {
                    that.setData({
                        pdbottom: '68',
                        lineH: 128
                    })
                } else {
                    that.setData({
                        pdbottom: '40',
                        lineH: 100
                    })
                }
            },
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (utils.is_define(this.data.cutImg)) {
            this.selectComponent("#loading").showLoadingCus();
            this.selectComponent('#pm').add(this.data.cutImg)
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    }
})